//引入接口
import API from '../../config/server'

Page({
  data:{
    voiceNav: [],
    navType: '187',
    hero: [],
    voiceDetail:[],
    voiceSrc: '',
    voiceName: '',
    voiceAuthor: ''
  },
  onLoad()
  {
    this.voiceList()
    this.voiceData(this.data.navType)
  },
  onReady(e)
  {
    //创建一个语音播放的对象
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  voiceList(){
    let that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: API.voicenav,
      method: 'GET',
      data: '',
      dataType: 'json',
      success: (result) => {
        const data = result.data.data;
        that.setData({
          voiceNav: data.dhty_e9
        })
        // console.log(result)
      },
      fail: (res) => {
       wx.showToast({
         title: '网络错误',
         icon: 'error',
       })
      },
      complete: (res) => {
        wx.hideLoading()
      },
    })
  },
  voiceData(id){
    let _this = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: API.voicedetail,
      method: 'GET',
      data: {id: id},
      dataType: 'json',
      success: (result) => {
        // console.log(result)
        const info = result.data;
        let detail = []
        if(info.data.yxpfyy_fe instanceof Array){
          detail = info.data.yxpfyy_fe
        }else{
          detail.push(info.data.yxpfyy_fe)
        }
        if(info.success){
          _this.setData({
            hero: info.data,
            voiceDetail: detail
          })
        }
      },
      fail: (res) => {
       wx.showToast({
         title: '网络错误',
         icon: 'error',
       })
      },
      complete: (res) => {
        wx.hideLoading()
      },
    })
  },
  changeNav(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      navType: type
    });
    this.voiceData(type)
  },
  play(e){
    let src = e.currentTarget.dataset.src,
        name = e.currentTarget.dataset.name,
        author = e.currentTarget.dataset.author
    this.setData({
      voiceName: name,
      voiceSrc: src,
      voiceAuthor: author
    })
    this.audioCtx.pause()
    this.audioCtx.play()
  }
})